package com.yulongtian.offers.day202212;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * https://leetcode.cn/problems/last-stone-weight?envType=study-plan&id=leetcode_75_level_1&plan=leetcode_75&plan_progress=y7sk2g7
 * 可
 *
 * @author yulongTian
 * @create 2022-12-27 11:49
 */
public class Test07 {
    public static void main(String[] args) {
        int[] stones = {2, 7, 4, 1, 8, 1, 1};
        System.out.println(lastStoneWeight(stones));
    }

    public static int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        for (int stone : stones) {
            priorityQueue.add(stone);
        }

        while (priorityQueue.size() > 1) {

            if (!priorityQueue.isEmpty()) {
                int temp1 = priorityQueue.poll();
                if (!priorityQueue.isEmpty()) {
                    int temp2 = priorityQueue.poll();
                    if (temp1 != temp2) {
                        priorityQueue.add(Math.abs(temp1 - temp2));
                    }
                    priorityQueue.comparator();
                }
            } else {
                break;
            }
        }


        return priorityQueue.isEmpty() ? 0 : priorityQueue.poll();
    }

}
